﻿namespace LinkGame.Models.Variations
{
    internal sealed class MovingCenterAsideVariation : ApplicableVariation
    {
        #region Protected Methods

        protected override void DoApply(Map map, Point p1, Point p2)
        {
            if (p1.X < map.Width / 2)
            {
                if (p2.X < map.Width / 2)
                {
                    var maxX = map.Width / 2;
                    if (p1.Y == p2.Y)
                    {
                        // 如果两者处于同一行，则需要计算p1和p2哪个靠中线最近，先移动靠中线最近的那个
                        var first = p1.X < p2.X ? p2 : p1;
                        var second = p1.X < p2.X ? p1 : p2;
                        MoveTilesOnPoint(map, first, MovingDirection.Left, maxX: maxX);
                        MoveTilesOnPoint(map, second, MovingDirection.Left, maxX: maxX);
                    }
                    else
                    {
                        // 如果不在同一样，分别向左移动
                        MoveTilesOnPoint(map, p1, MovingDirection.Left, maxX: maxX);
                        MoveTilesOnPoint(map, p2, MovingDirection.Left, maxX: maxX);
                    }
                }
                else
                {
                    var xBoundary = map.Width / 2;
                    MoveTilesOnPoint(map, p1, MovingDirection.Left, maxX: xBoundary);
                    MoveTilesOnPoint(map, p2, MovingDirection.Right, minX: xBoundary);
                }
            }
            else
            {
                if (p2.X < map.Width / 2)
                {
                    var xBoundary = map.Width / 2;
                    MoveTilesOnPoint(map, p1, MovingDirection.Right, minX: xBoundary);
                    MoveTilesOnPoint(map, p2, MovingDirection.Left, maxX: xBoundary);
                }
                else
                {
                    var minX = map.Width / 2;
                    if (p1.Y == p2.Y)
                    {
                        var first = p1.X < p2.X ? p1 : p2;
                        var second = p1.X < p2.X ? p2 : p1;
                        MoveTilesOnPoint(map, first, MovingDirection.Right, minX: minX);
                        MoveTilesOnPoint(map, second, MovingDirection.Right, minX: minX);
                    }
                    else
                    {
                        MoveTilesOnPoint(map, p2, MovingDirection.Right, minX: minX);
                        MoveTilesOnPoint(map, p1, MovingDirection.Right, minX: minX);
                    }
                }
            }
        }

        #endregion Protected Methods
    }
}